﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace DoNet.Common.Utilities
{
    public class PageHelper
    {
        #region Internet处理

        /// <summary>
        /// 得到指定url地址的网页内容
        /// </summary>
        /// <param name="url">url地址</param>
        /// <returns></returns>
        public static string ReadWebPage(string url)
        {
            System.IO.StreamReader sr = null;
            System.IO.Stream stream = null ;
            string webPage = string.Empty;
            try
            {
                if (!url.Substring(0, 7).Equals("http://"))
                {
                    url = "http://" + url;
                }
                System.Net.WebRequest request = System.Net.HttpWebRequest.Create(url);
                stream = request.GetResponse().GetResponseStream();
                sr = new System.IO.StreamReader(stream);
                webPage = sr.ReadToEnd();
            }
            catch
            {
                return "读取错误";
            }
            finally
            {
                if(sr!=null)
                    sr.Close();
                if(stream!=null)
                    stream.Close();
            }
            return webPage;
        }

        #endregion

        #region Html处理
        /// <summary>
        /// 处理HTML的显示
        /// </summary>
        /// <param name="origString">HTML内容</param>
        /// <returns>string</returns>
        public static string StripHtml(string origString)
        {
            StringBuilder outputString = new StringBuilder();
            Regex regularExpression = new Regex("<(.|\n)+?>", RegexOptions.IgnoreCase);

            outputString = new StringBuilder(regularExpression.Replace(origString, ""));

            outputString.Replace("<", "&lt;");
            outputString.Replace(">", "&gt;");

            return outputString.ToString();
        }
        #endregion Html处理

        #region cookie处理

        /// <summary>
        /// 设置cookie
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void SetCookie(Page page, string key, string value)
        {
            if (page.Request.Cookies[key] == null)
            {
                page.Response.Cookies.Add(new HttpCookie(key, value));
            }
            else
            {
                page.Response.Cookies[key].Value = value;
            }
        }

        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <param name="page">页面</param>
        /// <param name="key">key值</param>
        /// <returns></returns>
        public static string GetCookie(Page page, string key)
        {
            if (page.Request.Cookies[key] != null)
                return page.Request.Cookies[key].Value;
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// 获取url加密后的cookie
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetUrlEncodedCookie(Page page, string key)
        {
            if (page.Request.Cookies[key] != null)
                return page.Server.UrlDecode(page.Request.Cookies[key].Value);
            else
            {
                return string.Empty;
            }

        }

        /// <summary>
        /// 获取html加密后的cookie
        /// </summary>
        /// <param name="page"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetHtmlEncodedCookie(Page page, string key)
        {
            if (page.Request.Cookies[key] != null)
                return page.Server.HtmlDecode(page.Request.Cookies[key].Value);
            else
            {
                return string.Empty;
            }
        }

        public static void SetCookieExpires(Page page,TimeSpan span)
        {
            int n = page.Request.Cookies.Count;
            string key = string.Empty;
            for (int i = 0; i < n; i++)
            {
                key = page.Request.Cookies[i].Name;
                SetCookieExpires(page, key, span);
            }
        }

        public static void SetCookieExpires(Page page, DateTime dt)
        {
            int n = page.Request.Cookies.Count;
            string key = string.Empty;

            for (int i = 0; i < n; i++)
            {
                key = page.Request.Cookies[i].Name;
                SetCookieExpires(page, key, dt);
            }
        }

        public static void SetCookieExpires(Page page,string key,TimeSpan span)
        {
            DateTime dt = DateTime.Now.Add(span);
            SetCookieExpires(page, key, dt);
        }

        public static void SetCookieExpires(Page page, string key, DateTime dt)
        {
            page.Response.Cookies[key].Expires = dt;
        }

        public static void SetCookieDomain(Page page, string domain)
        {
            int n = page.Request.Cookies.Count;
            string key = string.Empty;

            for (int i = 0; i < n; i++)
            {
                key = page.Request.Cookies[i].Name;
                SetCookieDomain(page, key, domain);
            }
        }

        public static void SetCookieDomain(Page page, string key, string domain)
        {
            page.Response.Cookies[key].Domain = domain;
        }

        public static string GetCookieDomain(Page page, string key)
        {
            return page.Request.Cookies[key].Domain;
        }

        public static void RemoveCookies(Page page)
        {
            int n = page.Request.Cookies.Count;
            string key = string.Empty;
            for (int i = 0; i < n; i++)
            {
                key = page.Request.Cookies[i].Name;
                RemoveCookies(page, key);
            }
        }

        public static void RemoveCookies(Page page, string key)
        {
            page.Response.Cookies.Remove(key);
        }

        #endregion cookie处理

        #region Session处理

        public static void SetSession(Page page,string key,string value)
        {
            page.Session[key] = value;
        }

        public static string GetSession(Page page, string key)
        {
            if (page.Session[key] == null)
                return null;
            return page.Session[key].ToString();
        }

        #endregion

        #region Web控件数据绑定
        /// <summary>
        /// 将数据和DropDownList控件绑定
        /// </summary>
        /// <param name="ddlist">DropDownList控件</param>
        /// <param name="names">名称数组</param>
        /// <param name="values">值数组</param>
        /// <param name="defaultValue">缺省选择值</param>
        /// <param name="isAllowEmpty">是否允许为空</param>
        public static void DropDownListBind(System.Web.UI.WebControls.DropDownList ddlist, string[] names, string[] values, string defaultValue, bool isAllowEmpty)
        {
            bool isExistValue = false;
            ddlist.Items.Clear();

            //如果允许为空，则增加一空行
            if (isAllowEmpty)
            {
                ddlist.Items.Add(new System.Web.UI.WebControls.ListItem("", ""));
                if (defaultValue != null && defaultValue == "")
                    isExistValue = true;
            }

            //填充列表
            for (int i = 0; i < names.Length; i++)
            {
                ddlist.Items.Add(new System.Web.UI.WebControls.ListItem(names[i], values[i]));
                if (defaultValue != null && !isExistValue && defaultValue == values[i])
                    isExistValue = true;
            }

            //设置默认值
            if (defaultValue != null && isExistValue)
                ddlist.SelectedValue = defaultValue;
        }

        /// <summary>
        /// 将数据和DropDownList控件绑定
        /// </summary>
        /// <param name="ddlist">DropDownList控件</param>
        /// <param name="names">名称数组</param>
        /// <param name="values">值数组</param>
        /// <param name="isAllowEmpty">是否允许为空</param>
        public static void DropDownListBind(System.Web.UI.WebControls.DropDownList ddlist, string[] names, string[] values, bool isAllowEmpty)
        {
            ddlist.Items.Clear();

            //如果允许为空，则增加一空行
            if (isAllowEmpty)
            {
                ddlist.Items.Add(new System.Web.UI.WebControls.ListItem("", ""));
            }

            //填充列表
            for (int i = 0; i < names.Length; i++)
            {
                ddlist.Items.Add(new System.Web.UI.WebControls.ListItem(names[i], values[i]));
            }
        }

        /// <summary>
        /// 将数据和DropDownList控件绑定
        /// </summary>
        /// <param name="ddlist">DropDownList控件</param>
        /// <param name="keyValues">id，名称值对</param>
        /// <param name="isAllowEmpty">是否允许为空</param>
        public static void DropDownListBind(System.Web.UI.WebControls.DropDownList ddlist, Dictionary<string, string> keyValues, bool isAllowEmpty)
        {
            ddlist.Items.Clear();

            //如果允许为空，则增加一空行
            if (isAllowEmpty)
            {
                ddlist.Items.Add(new System.Web.UI.WebControls.ListItem("", ""));
            }

            //填充列表
            foreach (string key in keyValues.Keys)
            {
                ddlist.Items.Add(new System.Web.UI.WebControls.ListItem(keyValues[key], key));
            }
        }

        public static void DropDownListBind<T>(System.Web.UI.WebControls.DropDownList ddlist, IEnumerable<T> query, string valueName, string textName, bool isAllowEmpty)
        {
            ddlist.Items.Clear();

            if (query != null)
            {
                //如果允许为空，则增加一空行
                if (isAllowEmpty)
                {
                    ddlist.Items.Add(new System.Web.UI.WebControls.ListItem("", ""));
                }

                System.Data.DataTable dt = DataTableHelper.ToDataTable<T>(query);

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ddlist.Items.Add(new System.Web.UI.WebControls.ListItem(dt.Rows[i][textName].ToString(), dt.Rows[i][valueName].ToString()));
                }
            }
        }

        #endregion Web控件数据绑定
    }
}
